//
//  UIAlertController+Window.m
//  GomroBoss
//
//  Created by mac on 2020/8/21.
//  Copyright © 2020 cn.gomro.app. All rights reserved.
//

#import "UIAlertController+Window.h"
#import <objc/runtime.h>


//MARK: - UIAlertController
@interface UIAlertController (Private)
 
@property (strong,nonatomic) UIWindow *alertWindow;
 
@end
 
@implementation UIAlertController (Private)
 
@dynamic alertWindow;
 
- (void)setAlertWindow:(UIWindow *)alertWindow {
    objc_setAssociatedObject(self, @selector(alertWindow),alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
- (UIWindow *)alertWindow {
    return objc_getAssociatedObject(self, @selector(alertWindow));
}
 
@end

@implementation UIAlertController (Window)

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.alertWindow.hidden = YES;
    self.alertWindow = nil;
}
 
- (void)show {
    [self show:YES];
}
 
- (void)show:(BOOL)animated {
    @autoreleasepool {
        self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.alertWindow.rootViewController = [[UIViewController alloc] init];
        
        self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
        [self.alertWindow makeKeyAndVisible];
        [self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil];
    }
}

@end


//MARK: - UIImagePickerController
@interface UIImagePickerController (Private)
 
@property (strong,nonatomic) UIWindow *alertWindow;
 
@end
 
@implementation UIImagePickerController (Private)
 
@dynamic alertWindow;
 
- (void)setAlertWindow:(UIWindow *)alertWindow {
    objc_setAssociatedObject(self, @selector(alertWindow),alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
- (UIWindow *)alertWindow {
    return objc_getAssociatedObject(self, @selector(alertWindow));
}
 
@end

@implementation UIImagePickerController (Window)

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.alertWindow.hidden = YES;
    self.alertWindow = nil;
}
 
- (void)show {
    [self show:YES];
}
 
- (void)show:(BOOL)animated {
    @autoreleasepool {
        self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.alertWindow.rootViewController = [[UIViewController alloc] init];
        
        self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
        [self.alertWindow makeKeyAndVisible];
        [self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil];
    }
}
@end


//MARK: - QBImagePickerController
@interface QBImagePickerController (Private)
 
@property (strong,nonatomic) UIWindow *alertWindow;
 
@end
 
@implementation QBImagePickerController (Private)
 
@dynamic alertWindow;
 
- (void)setAlertWindow:(UIWindow *)alertWindow {
    objc_setAssociatedObject(self, @selector(alertWindow),alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
 
- (UIWindow *)alertWindow {
    return objc_getAssociatedObject(self, @selector(alertWindow));
}
 
@end

@implementation QBImagePickerController (Window)

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.alertWindow.hidden = YES;
    self.alertWindow = nil;
}
 
- (void)show {
    [self show:YES];
}
 
- (void)show:(BOOL)animated {
    @autoreleasepool {
        self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.alertWindow.rootViewController = [[UIViewController alloc] init];
        
        self.alertWindow.windowLevel = UIWindowLevelAlert + 1;
        [self.alertWindow makeKeyAndVisible];
        [self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil];
    }
}


@end
